<template>
	<view>
		<view class="dizhi" v-for="(item,idx) in list" :key="item.id" @click="select(item.id)">
			<view class="left" style="margin-left: 20rpx;">
				<view class="top">
					<text>{{item.username}}</text>
					<text style="padding: 0 20rpx 0 20rpx;">{{item.userphone}}</text>
					<u-tag :text="`${item.label}`" type="error" size="mini"></u-tag>
				</view>
				<view class="bot">
					{{item.useraddress}}
				</view>
			</view>
			<view class="right" style="margin-right: 20rpx;">
				<view class="btn">
					<u-button type="error" size="mini" @click.native.stop="del(item.id)">删除</u-button>
				</view>
				<view class="">
					<u-button type="primary" size="mini" @click.native.stop="edit(idx)">编辑</u-button>
				</view>
			</view>
		</view>
		<view class="add" @click="add">
			+ 新建收货地址
		</view>
	</view>
</template>

<script>
	import {
		addresslist,
		addressdel
	} from '../../request/app.js'
	export default {
		data() {
			return {
				list: []
			}
		},
		onLoad() {
			this.addressList()
		},
		methods: {
			addressList() {
				addresslist({
					uid: uni.getStorageSync('userinfo').uid
				}, {
					authorization: uni.getStorageSync('userinfo').token
				}).then(res => {
					console.log(res, '响应');
					if (res.data.code == 200) {
						this.list = res.data.list.map(item => {
							return {
								id: item.id,
								username: item.username,
								userphone: item.userphone,
								location: item.location,
								useraddress: item.useraddress,
								status: item.status,
								label: item.label
							}
						})
					}
				})
			},
			add() {
				uni.navigateTo({
					url: '/pages/newaddress/newaddress'
				})
			},
			edit(idx) {
				console.log(idx);
				const {
					id,
					username,
					userphone,
					location,
					useraddress,
					status,
					label
				} = this.list[idx]
				console.log(
					id,
					username,
					userphone,
					location,
					useraddress,
					status,
					label);
				uni.navigateTo({
					url: '/pages/newaddress/newaddress?id=' + id + '&username=' + username + '&userphone=' +
						userphone + '&useraddress=' + useraddress + '&status=' + status + '&label=' + label +
						'&location=' + location
				})
			},
			del(id) {
				// console.log(id);
				addressdel({
					id
				}, {
					authorization: uni.getStorageSync('userinfo').token
				}).then(res => {
					console.log(res, '地址删除');
					if (res.data.code == 200) {
						this.addressList()
					}
				})
			},
			select(id) {
				console.log(id);
				uni.navigateTo({
					url: '/pages/order/order?id=' + id
				})
			}
		}
	}
</script>

<style>
	.dizhi {
		display: flex;
		justify-content: space-between;
	}

	.dizhi .left,
	.dizhi .right {
		padding-top: 70rpx;
	}

	.dizhi .left .top {
		display: flex;
		padding-bottom: 20rpx;
	}

	.dizhi .right .btn {
		margin-bottom: 20rpx;
	}

	.add {
		width: 650rpx;
		height: 100rpx;
		border-radius: 50rpx;
		background-color: red;
		color: #fff;
		line-height: 100rpx;
		text-align: center;
		position: fixed;
		bottom: 30rpx;
		left: 55rpx;
	}
</style>
